package org.example.work.service.impl;

import org.example.work.mapper.CommentMapper;
import org.example.work.pojo.Comment;
import org.example.work.service.CommentService;
import org.example.work.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void addComment(Comment comment) {
        comment.setCreateTime(LocalDateTime.now());

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        comment.setUsername(username);
        comment.setLevel(1);
        comment.setParentCommentId(0);
        commentMapper.addComment(comment);
    }

    @Override
    public void replyComment(Comment comment) {
        comment.setCreateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        comment.setUsername(username);
        comment.setLevel(2);
        commentMapper.addComment(comment);
    }

    @Override
    public List<Comment> findCommentsByTaskId(Long taskid) {
        return commentMapper.findCommentsByTaskId(taskid);
    }



}